package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.entity.bo.FinanceInfoBo;
import com.mf.entity.vo.FinanceInfoVo;
import com.mf.mapper.FinanceInfoMapper;
import com.mf.service.IFinanceInfoService;
import io.github.heollhai.common.utils.MybatisPlusUtils;
import io.github.heollhai.common.utils.VerifyUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 财务信息服务实现类
 *
 * @author mf
 * @since 2024-09-01
 */
@Service
public class FinanceInfoServiceImpl extends ServiceImpl<FinanceInfoMapper, FinanceInfoBo> implements IFinanceInfoService {
    @Resource
    private FinanceInfoMapper financeInfoMapper;

    /**
     * 查询
     */
    @Override
    public FinanceInfoVo listByRosterId(Integer rosterId) {
        LambdaQueryWrapper<FinanceInfoBo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FinanceInfoBo::getRosterId, rosterId);

        return MybatisPlusUtils.changeDataClass(financeInfoMapper.selectOne(wrapper), FinanceInfoVo.class);
    }

    /**
     * 新增|编辑
     */
    @Override
    public Integer insert(FinanceInfoVo financeInfoVo) {
        final LambdaQueryWrapper<FinanceInfoBo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FinanceInfoBo::getRosterId, financeInfoVo.getRosterId());
        FinanceInfoBo financeInfoBos = financeInfoMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(financeInfoBos)) {
            financeInfoBos = new FinanceInfoBo();
        }
        BeanUtils.copyProperties(financeInfoVo, financeInfoBos);
        if (VerifyUtils.isEmpty(financeInfoBos.getId())) {
            return financeInfoMapper.insert(financeInfoBos);
        }
        return financeInfoMapper.updateById(financeInfoBos);
    }
}
